// pages/sceneDetail/index.js
const api = require('../../config/api')
const request = require('./../../utils/request')
let app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    taskDate: '',
    trainingId: '', // 路径ID
    trainingDetail: {}, // 路径详情
    taskList: [],
    isVacant: false, // 是否有任务空缺
    today: new Date(),
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      trainingId: options.trainingId,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.calendar = this.selectComponent('#calendar')
  },
  // 路径信息
  getTrainingInfo: async function () {
    const { trainingId } = this.data
    const patientInfo = wx.getStorageSync('patientInfo')
    const res = await request.get(
      api.trainingDate,
      {
        patientId: patientInfo.id,
        trainingId: trainingId,
      },
      { loading: false }
    )
    if (res.httpStatus === 200) {
      this.setData({
        trainingDetail: res.data,
      })
    }
  },
  // 查询路径某日的任务列表
  getTaskList: async function () {
    const { trainingId, taskDate, isVacant } = this.data
    let hasVacant = isVacant
    if (taskDate) {
      const res = await request.get(api.trainingDayDetail, {
        trainingId,
        taskDate,
      })
      if (res.httpStatus === 200) {
        res.data.map((item) => {
          if (item.state === 3) {
            hasVacant = true
          }
        })
        this.setData({
          taskList: res.data,
          isVacant: hasVacant,
        })
      }
    }
  },
  /**
   * 日期选中变化
   * @param {*} e
   */
  change(e) {
    this.setData({
      taskDate: e.detail,
      isVacant: false,
    })

    this.getTaskList()
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (this.calendar) {
      this.calendar.dateInit()
    }
    this.getTrainingInfo()
    this.getTaskList()
  },

  // 跳转报告详情
  handleReport() {
    wx.navigateTo({
      url: `/pages/sceneTrainReport/index?trainingId=${this.data.trainingId}`,
    })
  },
  // 跳转任务详情
  handleTaskDetail(e) {
    console.log(this.data.today.valueOf(), new Date(this.data.taskDate))
    console.log(e.currentTarget.dataset.item)

    let {
      detailId,
      questionnaireId,
      trainingType,
      state,
      questionnaireRecordId,
    } = e.currentTarget.dataset.item
    let { trainingId, trainingDetail, taskDate } = this.data

    let today = new Date().valueOf()
    let currentDay = new Date(taskDate).valueOf()

    if (currentDay > today) {
      wx.showToast({
        title: '未到打卡时间！',
        icon: 'none',
      })
      return
    }
    app.globalData.finishTaskEntrance = 4
    if (Number(trainingType) === 1) {
      wx.navigateTo({
        url: '/pages/sceneTaskDetail/index?id=' + detailId,
      })
    } else {
      if (state === 4) {
        wx.navigateTo({
          url:
            '/pages/selfAssessment/question/result?id=' +
            questionnaireRecordId +
            '&pathId=' +
            trainingId +
            '&isResultList=isResult',
        })
        return
      }
      // 已完结，不可补卡
      if (trainingDetail.state === 3) {
        wx.showToast({
          title: '场景训练已完结，不可评估',
          icon: 'none',
        })
        return
      }
      wx.navigateTo({
        url:
          '/pages/selfAssessment/question/index?key=1&id=' +
          questionnaireId +
          '&pathId=' +
          trainingId +
          '&taskFlowId=' +
          detailId,
      })
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
})
